<script lang="ts" setup name="ndeReportRejectPart">
import { api, getListByConditionsWithPage, urls } from '@/index';
import type {
  DialogModel,
  NdeReportRejectPartQueryCondition,
  NdeReportRejectPart,
  DrawerModel
} from '@/index.d';
import { useRequest } from 'vue-request';
import RejectPartDetailDialog from './RejectPartDetailDialog.vue';
import NdeDefctDraw from './NdeDefctDraw.vue';
import { makeMethodList, ownerList, partTypeList, provinceList, testStageList } from './optionList';
const title = '缺陷工件';
const queryCondition = reactive<NdeReportRejectPartQueryCondition>({
  deleted: false,
  change: false,
  currentPage: 1,
  pageSize: 20
});

const resetQueryCondition = () => {
  queryCondition.currentPage = 1;
  queryCondition.pageSize = 20;
  queryCondition.deleted = false;
  delete queryCondition.owner;
  delete queryCondition.province;
  delete queryCondition.reportReference;
  delete queryCondition.testMethod;
  delete queryCondition.partType;
  delete queryCondition.makeMethod;
  delete queryCondition.productionReference;
  delete queryCondition.partName;
  delete queryCondition.partReference;
};

const {
  data: getNdeReportRejectPartListData,
  run: getNdeReportRejectPartListRun,
  loading: getNdeReportRejectPartListLoading
} = useRequest(() => getListByConditionsWithPage(urls.ndeReportRejectPart, queryCondition));

const handleResetQuery = () => {
  resetQueryCondition();
  getNdeReportRejectPartListRun();
};

const detailDialogModel = reactive<DialogModel<NdeReportRejectPart>>({
  visible: false,
  title,
  type: 'view',
  id: 0,
  change: false
});

const ndeDefectDrawerModel = reactive<DrawerModel<NdeReportRejectPart>>({
  visible: false,
  type: 'view',
  id: 0,
  change: false,
  title: ''
});

const viewNdeDefect = (row: NdeReportRejectPart) => {
  ndeDefectDrawerModel.id = row.id as number;
  ndeDefectDrawerModel.title =
    (row.reportReference as string) + ' - ' + (row.partReference as string);
  ndeDefectDrawerModel.data = row;
  ndeDefectDrawerModel.visible = true;
};

watch(
  () => detailDialogModel.visible,
  (value: boolean) => {
    if (!value && detailDialogModel.change) {
      getNdeReportRejectPartListRun();
      detailDialogModel.change = false;
    }
  }
);

const updatePartInfoToDefect = () => {
  const res = api({
    url: urls.ndeReportNdeDefect + '/updatePartInfoToDefect'
  });
};
</script>
<template>
  <HqContainerTable
    :base-url="urls.ndeReportReviewRecord"
    :title="title"
    :loading="getNdeReportRejectPartListLoading"
    :total="getNdeReportRejectPartListData?.total"
    :table-data="getNdeReportRejectPartListData?.records"
    v-model:pageSize="queryCondition.pageSize"
    v-model:current-page="queryCondition.currentPage"
    @handle-query="getNdeReportRejectPartListRun"
    @handle-reset-query="handleResetQuery"
    @handle-reload="getNdeReportRejectPartListRun"
    v-model:detail-dialog-model="detailDialogModel"
    :operate-column="true"
  >
    <template #search>
      <hq-col kind="search">
        <el-select
          v-model="queryCondition.testMethod"
          clearable
          placeholder="请选择方法"
          @change="getNdeReportRejectPartListRun"
        >
          <HqNdeMethodOptions />
        </el-select>
      </hq-col>
      <hq-col kind="search">
        <el-select
          v-model="queryCondition.partType"
          clearable
          placeholder="请选择工件类型"
          @change="getNdeReportRejectPartListRun"
        >
          <el-option v-for="item in partTypeList" :value="item" :label="item" :key="item" />
        </el-select>
      </hq-col>
      <hq-col kind="search">
        <el-select
          v-model="queryCondition.makeMethod"
          clearable
          placeholder="请选择加工方式"
          @change="getNdeReportRejectPartListRun"
        >
          <el-option v-for="item in makeMethodList" :value="item" :label="item" :key="item" />
        </el-select>
      </hq-col>
      <hq-col kind="search">
        <el-input
          v-model="queryCondition.productionReference"
          placeholder="请输入生产令号"
          clearable
          @keydown.enter="getNdeReportRejectPartListRun"
        />
      </hq-col>
      <hq-col kind="search">
        <el-input
          v-model="queryCondition.partName"
          placeholder="请输入工件名称"
          clearable
          @keydown.enter="getNdeReportRejectPartListRun"
        />
      </hq-col>
      <hq-col kind="search">
        <el-input
          v-model="queryCondition.partReference"
          placeholder="请输入工件编号"
          clearable
          @keydown.enter="getNdeReportRejectPartListRun"
        />
      </hq-col>
    </template>
    <template #button>
      <el-button type="primary" icon="Refresh" @click="updatePartInfoToDefect">
        同步工件信息
      </el-button>
    </template>
    <template #tableColumns>
      <HqTableColumn label="管理缺陷" :min-width="100">
        <template #default="{ row }">
          <el-button link type="success" size="default" @click="viewNdeDefect(row)">
            管理缺陷
          </el-button>
        </template>
      </HqTableColumn>
      <HqTableColumn label="报告附件" :min-width="250">
        <template #default="{ row }">
          <el-link
            v-if="row.reportAttachmentLink"
            type="primary"
            :href="row.reportAttachmentLink"
            target="_blank"
          >
            {{ row.reportReference }}
          </el-link>
          <span v-else> {{ row.reportReference }}</span>
        </template>
      </HqTableColumn>
      <HqTableColumn label="检测方法" prop="testMethod" :min-width="100" />
      <HqTableColumn label="检测结论" prop="testResult" :min-width="150" />
      <HqTableColumn label="生产令号" prop="productionReference" :min-width="150" />
      <HqTableColumn label="工件名称" prop="partName" :min-width="500" />
      <HqTableColumn label="工件编号" prop="partReference" :min-width="300" />
      <HqTableColumn label="工件类型" prop="partType" :min-width="200" />
      <HqTableColumn label="加工工艺" prop="makeMethod" :min-width="200" />
      <HqTableColumn label="检测时机" prop="testStage" :min-width="200" />
      <HqTableColumn label="检测日期" prop="testDate" :min-width="200" />
    </template>
  </HqContainerTable>
  <RejectPartDetailDialog
    v-model:dialog-model="detailDialogModel"
    v-if="detailDialogModel.visible"
  />
  <NdeDefctDraw v-model:drawer-model="ndeDefectDrawerModel" v-if="ndeDefectDrawerModel.visible" />
</template>
<style lang="scss" scoped></style>
